<template>
  <div id="globalHeader">
    <a-row class="grid-demo" align="center" :wrap="false">
      <a-col flex="auto">
        <a-menu
          mode="horizontal"
          :selected-keys="selectedKeys"
          @menu-item-click="doMenuClick"
        >
          <a-menu-item
            key="0"
            :style="{ padding: 0, marginRight: '38px' }"
            disabled
          >
            <div class="title-bar">
              <img class="logo" src="../assets/OIP-C.jpg" />
              <div class="title">判题OJ系统</div>
            </div>
          </a-menu-item>
          <a-menu-item v-for="item in visibleRoutes" :key="item.path">
            {{ item.name }}
          </a-menu-item>
        </a-menu>
      </a-col>
      <a-col flex="100px">
        <div
          v-if="
            store.loginUser.userRole == accessEnum.NOT_LOGIN ||
            store.loginUser.userRole == null
          "
        >
          <a href="#" @click="login"> 未登录 </a> |
          <a href="#" @click="register"> 注册 </a>
        </div>
        <div v-else>
          {{ store.loginUser?.userName }} |
          <a href="#" @click="logout">退出</a>
        </div>
      </a-col>
    </a-row>
  </div>
</template>

<script setup lang="ts">
import { routes } from "@/router/routes";
import { useRouter } from "vue-router";
import { computed, reactive, ref } from "vue";
import { useStore } from "@/store/index";
import checkAccess from "@/access/checkAccess";
import accessEnum from "@/access/accessEnum";
import { Message } from "@arco-design/web-vue";
import { UserControllerService } from "../../generated";

const router = useRouter();
const store = useStore();
const loginUser = store.loginUser;

// 路由切换后，更新导航栏的高亮状态
const selectedKeys = ref();
router.afterEach((to, from, failure) => {
  selectedKeys.value = [to.path];
});

// 导航栏切换路由
const doMenuClick = (key: string) => {
  router.push({
    path: key,
  });
};

// 根据用户权限过滤掉隐藏路由和无权限路由
const visibleRoutes = computed(() => {
  return routes.filter((item, index) => {
    if (item.meta?.hidden) {
      return false;
    }
    if (!checkAccess(loginUser, item?.meta?.assess as string)) {
      return false;
    }
    return true;
  });
});

// 用户登录
function login() {
  router.push({
    path: "/user/login",
    replace: true,
  });
}

// 用户注册
function register() {
  router.push({
    path: "/user/register",
    replace: true,
  });
}

// 用户退出
async function logout() {
  const res = await UserControllerService.userLogoutUsingPost();
  if (res.code === 0) {
    Object.keys(store.loginUser).forEach((key: string) => {
      store.loginUser[key as keyof typeof store.loginUser] = null;
    });
    router.push({
      path: "/",
      replace: true,
    });
    Message.success({
      content: "退出成功！",
    });
  } else {
    Message.error({
      content: "退出成功！",
    });
  }
}
</script>

<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}

.title {
  color: #444;
  margin-left: 16px;
  font-weight: 700;
}

.logo {
  height: 48px;
}
</style>
